package org.example.seckill.controller;

import com.sun.org.apache.regexp.internal.RE;
import net.sf.jsqlparser.Model;
import org.example.seckill.bean.User;
import org.example.seckill.bean.goods;
import org.example.seckill.bean.seckillGoods;
import org.example.seckill.service.UserService;
import org.example.seckill.service.goodsService;
import org.example.seckill.service.seckillGoodsService;
import org.example.seckill.util.timeUtil;
import org.example.seckill.vo.RespBean;
import org.example.seckill.vo.RespBeanEnum;
import org.example.seckill.vo.goodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author woopsion
 * @date 2025-06-29
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    
    @Autowired
    UserService userService;
    @Autowired
    goodsService goodsService;
    @Autowired
    seckillGoodsService seckillGoodsService;
    
    @Autowired
    private RedisTemplate redisTemplate;
//    @RequestMapping("toList")
//    public RespBean toList(
//            @CookieValue("userTicket") String ticket,
//            HttpServletRequest request, HttpServletResponse response){
    @RequestMapping("/toList")
    public RespBean toList(User user){
//        if( !StringUtils.hasText(ticket)){
//            return RespBean.error(RespBeanEnum.Error);
//        }
//        User user = userService.getUserByCookieTicket(ticket,request,response) ;
        if(user==null){ return   RespBean.error(RespBeanEnum.LOGIN_ERROR);}
        //缓存加速
        List<goods> redisData = (List<goods>)redisTemplate.opsForValue().get("goods:toList");
        
        
        List<goods> goods = goodsService.goodsList();
        redisTemplate.opsForValue().set("goods:toList",goods,60, TimeUnit.SECONDS);
        return RespBean.success(goods);
    }
    
    @GetMapping("/seckill")
    public RespBean getSeckillGoods(User user){

        if(user == null){             
            return RespBean.error(RespBeanEnum.AUTHENTICATION_ERROR);    
        }
        //缓存加速         
        List<goods> redisData = (List<goods>)redisTemplate.opsForValue().get("goods:toList");
        List<goods> goods = goodsService.goodsList();         
        redisTemplate.opsForValue().set("goods:toList",goods,60, TimeUnit.SECONDS);
        return RespBean.success(goods);
        
    }
    @GetMapping("/detail/{goodsId}")
    public RespBean getGoodsDetail(
            User user,
            @PathVariable("goodsId") Long goodsId){
        //返回容器
        //检验用户权限
        if (user == null){
            return RespBean.error(RespBeanEnum.AUTHENTICATION_ERROR);
        }
        goods o = (goods)redisTemplate.opsForValue().get("goods:detail:" + goodsId);
        
        if (o == null){
            //刷新redis
            o= goodsService.getById(goodsId);
            redisTemplate.opsForValue().set("goods:detail:" + goodsId,o,60, TimeUnit.SECONDS);
        }
//        Map<String,Object> retrurnMap = new HashMap<>();
//        goods goods = goodsService.getById(goodsId);
//        seckillGoods vo = seckillGoodsService.getById(goodsId);
//        int seckillStatusByGoodsId = seckillGoodsService.getSeckillStatusByGoodsId(goodsId).getStatus();
//        retrurnMap.put("goodsStatus",seckillStatusByGoodsId);        
//        if (seckillStatusByGoodsId == 1){
//            Long toStartTime = timeUtil.DateOperator(new Date(), vo.getStartDate());
//            retrurnMap.put("toStartTime",toStartTime);
//        }else {retrurnMap.put("toStartTime",-1);}
//        if (seckillStatusByGoodsId == 2 ){
//            Long toEndTime = timeUtil.DateOperator(new Date(), vo.getEndDate());
//            retrurnMap.put("toEndTime",toEndTime);
//        }else {retrurnMap.put("toEndTime",-1);}
//        retrurnMap.put("goodsTitle",goods.getGoodsTitle());
//        retrurnMap.put("goodsName",goods.getGoodsName());
//        retrurnMap.put("goodsKillPrice",vo.getSeckillPrice());
//        retrurnMap.put("goodsStartTime",vo.getStartDate());
//        retrurnMap.put("goodsEndTime",vo.getEndDate());
        return RespBean.success(o);
    }
    
}
